<% content_for :head_bottom do %>
	<%= javascript_include_tag 'ace' %>
	<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
	<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<% end %>

<p>
	<%=t('scripts.rules_summary_html', {:full_rules_link => link_to(t('scripts.rules_summary_link_to_full'), help_code_rules_path)})%>
</p>

<% # ignore userscripts.org sync
if !script_version.script.script_sync_type_id.nil? and script_version.script.script_sync_source_id != 2 %>
	<p>
		<%=t('scripts.syncing_notice_html', {
			:syncing_notice_sync_type => link_to(
				t(
					script_version.script.script_sync_type_id == 1 ? 'scripts.syncing_notice_sync_type_manual' : 'scripts.syncing_notice_sync_type_automatic',
					{:sync_identifier => script_version.script.sync_identifier}
				),
				script_sync_path(script_version.script_id)
			)
		})%>
	</p>
<% end %>

<%=form_for(script_version.script.new_record? ? script_version : [script_version.script, script_version], {:html => {:enctype => 'multipart/form-data'}}) do |s| %>

	<% # remember previously chosen overrides
	[:version_check_override, :add_missing_version, :namespace_check_override, :add_missing_namespace, :minified_confirmation].each do |override|
		if script_version.send(override) %>
			<%=s.hidden_field(override)%>
		<% end
	end

	if !script_version.errors.empty? or !script_version.script.errors.empty? %>
		<div class="alert">
			<p><%=t('common.error_preamble')%></p>
			<ul class="errors">
				<% displayed_errors = []
				script_version.screenshots.each do |screenshot|
					screenshot.errors.each do |attr, msg|
						displayed_errors << attr.to_s.humanize + ' ' + msg
					end
				end
				script_version.errors.each do |attr, msg|
					# need custom handling for warnings
					next if msg.starts_with?('warning-') || attr.to_s.include?('screenshot')
					if attr == :base
						displayed_errors << msg
					elsif msg.is_a?(String)
						displayed_errors << "#{attr.to_s.capitalize} #{msg}"
					else
						displayed_errors << script_version.errors.generate_message(attr, msg)
					end
				end
				script_version.script.errors.full_messages.each do |m|
					displayed_errors << m
				end
				displayed_errors.uniq.each do |err| %>
					<li><%=err%></li>
				<% end
				script_version.warnings.each do |w| %>
					<li><%
						case w
							when :version_not_incremented %>
								<%=t('scripts.warning_version_not_incremented_html')%><br>
								<label for="script_version_version_check_override"><%=t('scripts.override_warning_checkbox_label')%></label> <%=s.check_box(:version_check_override, {}, true, false)%>
							<% when :version_missing %>
								<%=t('scripts.warning_version_missing_html')%><br>
								<label for="script_version_add_missing_version"><%=t('scripts.warning_version_missing_generate_automatically_html')%></label> <%=s.check_box(:add_missing_version, {}, true, false)%>
							<% when :namespace_missing %>
								<%=t('scripts.warning_namespace_missing_html')%><br>
								<label for="script_version_add_missing_namespace"><%=t('scripts.warning_namespace_missing_generate_automatically_html')%></label> <%=s.check_box(:add_missing_namespace, {}, true, false)%>
							<% when :namespace_changed %>
								<%=t('scripts.warning_namespace_changed_html')%><br>
								<label for="script_version_namespace_check_override"><%=t('scripts.override_warning_checkbox_label')%></label> <%=s.check_box(:namespace_check_override, {}, true, false)%>
							<% when :potentially_minified %>
								<%=t('scripts.warning_potentially_minified_html', {:allowed_requires_link => link_to(t('scripts.warning_potentially_minified_allowed_requires_link'), help_external_scripts_path())})%><br>
								<label for="script_version_minified_confirmation"><%=t('scripts.override_warning_checkbox_label')%></label> <%=s.check_box(:minified_confirmation, {}, true, false)%>
							<% else
								raise "Unknown warning #{w}"
						end %>
					</li><%
				end %>
			</ul>
		</div>
	<% end %>

	<% if @script.library? %>
		<div class="form-control">
			<label for="library-name"><%=t('activerecord.attributes.script.name')%></label><br>
			<input id="library-name" name="name" value="<%=@script.name%>" maxlength="100">
		</div>
		<div class="form-control">
			<label for="library-description"><%=t('activerecord.attributes.script.description')%></label><br>
			<input id="library-description" name="description" value="<%=@script.description%>" maxlength="500">
		</div>
	<% end %>
	<div class="form-control">
		<label for="code"><%=t('activerecord.attributes.script.code')%></label> <span class="label-note" style="display: none"><input type="checkbox" id="enable-source-editor-code" name="enable-source-editor-code" <%if params['enable-source-editor-code']%>checked<%end%> class="enable-source-editor" data-related-editor="script_version_code"><label for="enable-source-editor-code" class="checkbox-label"><%=t('scripts.enable_source_code_editor_label')%></label></span><br>
		<%=s.text_area(:code)%>
		<div><%=t('scripts.upload_label')%> <input type="file" name="code_upload" id="code-upload" accept="text/javascript,application/javascript"></div>
	</div>
	<% if !script_version.script.new_record? %>
		<div class="form-control">
			<label for="changelog"><%=t('activerecord.attributes.script.changelog')%></label> <span class="label-note"><%=t('scripts.changelog_note')%></span><br>
			<%=s.text_field(:changelog, :maxlength => 500, :size => nil)%>
		</div>
	<% end %>
	<div class="form-control radio-group">
		<label><%=t('activerecord.attributes.script.type')%></label><br>
		<% ScriptType.all.each do |st| %>
			<%=radio_button('script', 'script_type_id', st.id)%> <label class="radio-label" for="script_script_type_id_<%=st.id%>"><%=t("scripts.type_#{st.id}_html", site_name: site_name)%></label><br>
		<% end %>
	</div>
	<div class="form-control radio-group">
		<label><%=t('activerecord.attributes.script.approve_redistribution')%></label><br>
		<%=t('scripts.approve_redistribution_explanation', site_name: site_name)%><br>
		<%= radio_button('script', :approve_redistribution, nil)%> <label for="script_approve_redistribution" class="radio-label"><%=t('scripts.approve_redistribution_use_user_setting', user_setting: script_version.script.user.approve_redistribution.nil? ? t('scripts.approve_redistribution_use_user_setting_not_set') : t("users.approve_redistribution_#{script_version.script.user.approve_redistribution}"))%></label><br>
		<%= radio_button('script', :approve_redistribution, true)%> <label for="script_approve_redistribution_true" class="radio-label"><%=t('scripts.approve_redistribution_true')%></label><br>
		<%= radio_button('script', :approve_redistribution, false)%> <label for="script_approve_redistribution_false" class="radio-label"><%=t('scripts.approve_redistribution_false')%></label>
	</div>
	<% if !script_version.script.sensitive || script_version.script.adult_content_self_report %>
		<div class="form-control radio-group">
			<label><%=t('scripts.adult_content')%></label><br>
			<%=t('scripts.adult_content_self_report_explanation', site_name: site_name)%><br>
			<%= check_box('script', :adult_content_self_report) %>
			<label for="script_adult_content_self_report" class="radio-label"><%=t('scripts.adult_content_self_report')%></label>
		</div>
	<% else %>
		<div class="form-control radio-group">
			<label><%=t('scripts.adult_content')%></label><br>
			<%=t('scripts.not_adult_content_self_report_explanation', site_name: site_name)%><br>
			<%= check_box('script', :not_adult_content_self_report, checked: @script.not_adult_content_self_report_date.present?) %>
			<label for="script_not_adult_content_self_report_date" class="radio-label"><%=t('scripts.not_adult_content_self_report_html')%></label>
		</div>
	<% end %>
	<div class="form-control radio-group">
		<label><%=t('activerecord.attributes.script.locale')%></label> <span class="label-note"><%=t('scripts.locale_explanation_html')%></span><br>
		<select name="script[locale_id]">
			<option value="" <%if @script.locale_id.nil?%>selected<%end%>><%=t('scripts.locale_auto_select')%></option>
			<option disabled>---</option>
			<% Locale.where(:ui_available => true).each do |locale| %>
				<option value="<%=locale.id%>" <%if @script.locale_id == locale.id%>selected<%end%>><%=locale.display_text%></option>
			<% end %>
			<option disabled>---</option>
			<% Locale.all.each do |locale| %>
				<option value="<%=locale.id%>"<%if @script.locale_id == locale.id%>selected<%end%>><%=locale.display_text%></option>
			<% end %>
		</select>
	</div>

	<% # default first
	script_version.localized_attributes_for('additional_info').sort{|a,b|
		next -1 if a.attribute_default && !b.attribute_default
		next 1 if !a.attribute_default && b.attribute_default
		next 0
	}.each_with_index do |la, index| %>
		<%=render :partial => 'additional_info', :locals => {:la => la, :index => index}%>
	<% end %>

	<div class="form-control">
		<button id="add-additional-info" name="add-additional-info" value="1" type="submit"><%=t('scripts.additional_info_add_localized')%></button>
	</div>

	<div class="form-control screenshots-controls">
		<label><%=t('activerecord.attributes.script.screenshots')%></label> <span class="label-note"><%=t('scripts.screenshots_explanation', {:size => number_to_human_size(Rails.configuration.screenshot_max_size), :number => Rails.configuration.screenshot_max_count})%></span><br>
		<% @current_screenshots.each_with_index do |screenshot, i| %>
			<div class="screenshot-control">
				<a href="<%=screenshot.screenshot.url%>" class="fancybox" rel="fancybox-screenshots"><%= image_tag screenshot.screenshot.url(:thumb) %></a>
				<input maxlength="500" size="50" id="edit-screenshot-caption-<%=i%>" class="edit-screenshot-captions" name="edit-screenshot-captions[]" placeholder="<%=t('activerecord.attributes.script.screenshot_caption')%>" value="<%=screenshot.caption%>"><br>
				<input id="remove-screenshot-<%=screenshot.id%>" name="remove-screenshot-<%=screenshot.id%>" type="checkbox"><label class="checkbox-label" for="remove-screenshot-<%=screenshot.id%>"><%=t('scripts.screenshots_remove')%></label>
			</div>
		<% end %>
		<% 5.times do |i| %>
			<div class="add-screenshot-control">
				<label class="radio-label" for="add-screenshot-<%=i%>"><%=t('scripts.screenshots_add')%></label> <input type="file" id="add-screenshot-<%=i%>" class="add-screenshots" name="screenshots[]"> <input maxlength="500" size="50" id="add-screenshot-caption-<%=i%>" class="add-screenshot-captions" name="screenshot-captions[]" placeholder="<%=t('activerecord.attributes.script.screenshot_caption')%>"><br>
			</div>
		<% end %>
	</div>

	<p>
		<%=s.submit(script_version.script.new_record? ? t('scripts.post_new_script') : t('scripts.post_updated_script'))%>
	</p>
<% end %>
